#!/bin/bash


# $Header$
# Author: yang.jin@ebay.com

export LANG=C
#export PATH=/bin:/usr/bin:/sbin:/usr/sbin

if [ $# -ne 1 ]; then
	echo $(basename $0) version >&2; exit 1
fi

version=$1

echo -e "\n\n###### Verifying if BIND ${version} /opt/bind-${version} is installed...\t\t\t\c"
if [ -d /opt/bind-${version} ]; then
	echo "YES"
	echo -e "\tError: /opt/bind-${version} exists. Rename it and rerun the tool if needed." >&2
	exit 1
else
	echo "Passed"
fi

echo -e "\n\n###### Verifying if source code directory /var/tmp/bind-${version} exists...\t\t\c"
if [ ! -d /var/tmp/bind-${version} ]; then
	echo "NO"
	echo -e "\n\n###### Verifying if source code tar ball /var/tmp/bind-${version}.tar.gz exists...\t\c"
	if [ -f /var/tmp/bind-${version}.tar.gz ]; then
		echo "YES"
		echo -e "\n\n###### Extracting source codes to /var/tmp/bind-${version}...\t\t\t\t\c"
		if [ "X$(uname -s)" == "XLinux" ]; then
			cd /var/tmp; tar xzf bind-${version}.tar.gz
			if [ $? -eq 0 ]; then echo Succeeded; else echo Failed; fi
		else
			if [ "X$(uname -s)" == "XSunOS" ]; then
				cd /var/tmp; gzcat bind-${version}.tar.gz | tar xf -
				if [ $? -eq 0 ]; then echo Succeeded; else echo Failed; fi
			else
				echo "Failed"
				echo -e "Error: O.S. $(uname -s) not supported." >&2
				exit 2
			fi
		fi
	else
		echo "NO"
		echo -e "Error: Either source code directory or tar ball doesn't exist." >&2
		echo -e "Copy source code tar ball from unixmon:/gis/itunix_repo/BIND." >&2
		exit 3
	fi
else
	echo "YES"
fi

cd /var/tmp/bind-${version}
if [ $? -ne 0 ]; then echo "/var/tmp/bind-${version} doesn't exist!" >&2; exit 4; fi

echo -e "\n\n###### Generating configuration files for compiling BIND ${version}..."
echo -e "\t--- Outputs are written to LOG.configure"
./configure  -sysconfdir=/etc/named  -prefix=/opt/bind-${version} -disable-ipv6  -enable-threads -with-openssl=no >> LOG.configure
if [ $? -ne 0 ]; then exit $?; fi
echo -e "\t--- Generating configuration succeeded"

echo -e "\n\n###### Compiling BIND ${version}..."
echo -e "\t--- Outputs are written to LOG.make"
make >> LOG.make
if [ $? -ne 0 ]; then exit $?; fi
echo -e "\t--- Code compilation succeeded"

echo -e "\n\n###### Installing BIND ${version}..."
echo -e "\t--- Outputs are written to LOG.make-install"
make install >> LOG.make-install
if [ $? -ne 0 ]; then exit $?; fi
echo -e "\t--- Code installation succeeded"

echo -e "\n\n###### Creating directory /opt/bind-${version}/var/run/named...\t\t\t\t\c"
if [ ! -d /opt/bind-${version}/var/run/named ]; then
	mkdir -p /opt/bind-${version}/var/run/named; chown named:named /opt/bind-${version}/var/run/named
	if [ $? -eq 0 ]; then echo Succeeded; else echo Failed; fi
else
	echo "Skipped"
fi

echo -e "\n\n###### Creating a link /opt/bind pointing to /opt/bind-${version}...\t\t\t\c"
if [ ! -L /opt/bind ]; then
	ln -s /opt/bind-${version} /opt/bind
	if [ $? -eq 0 ]; then echo Succeeded; else echo Failed; fi
	echo
else
	echo "Skipped"
	echo -e "Warning:\n\t/opt/bind exists - \n\t$(ls -ald /opt/bind)\n" >&2
fi

